package com.xoa.service.supervision.impl;

import com.xoa.dao.supervision.SupervisionApplyMapper;
import com.xoa.dao.supervision.SupervisionMapper;
import com.xoa.model.supervision.Supervision;
import com.xoa.model.supervision.SupervisionApply;
import com.xoa.model.users.Users;
import com.xoa.service.supervision.SupervisionApplyService;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 张丽军 on 2017/7/29.
 */
@Service
public class SupervisionApplyServiceImpl implements SupervisionApplyService{

    @Autowired
    SupervisionApplyMapper supervisionApplyMapper;

    @Autowired
    SupervisionMapper supervisionMapper;

    /**
     * 创建作者：张丽军
     * 创建日期：2017-08-04
     * 方法介绍：督办任务状态接口
     * @param request
     * @param supervisionApply
     * @return
     */
    @Transactional
    public ToJson<SupervisionApply> insertSupApply(HttpServletRequest request,SupervisionApply supervisionApply){
        ToJson<SupervisionApply> toJson=new ToJson<SupervisionApply>();
        try{
            Supervision supervision=new Supervision();
            String userId=(String) request.getSession().getAttribute("userId");
            supervisionApply.setCreaterId(userId);
            String currentTime=DateFormat.getCurrentTime();
            supervisionApply.setCreateTime(currentTime);
			int abc=supervisionApply.getType();
			switch (abc) {
				case 1:
					if(supervisionApply.getStatus()==0){
						Supervision sup=new Supervision();
						sup.setSid(supervisionApply.getSupId());
						sup.setStatus(2);
						int a=supervisionMapper.updateByPrimaryKeySelective(sup);
						if(a<1){
							return toJson;
						}
					}
					break;
				case 2:
					if(supervisionApply.getStatus()==0){
						Supervision sup=new Supervision();
						sup.setSid(supervisionApply.getSupId());
						sup.setStatus(4);
						int e=supervisionMapper.updateByPrimaryKeySelective(sup);
						if(e<1){
							return toJson;
						}
					}
					break;
				case 3:
					if(supervisionApply.getStatus()==0){
						Supervision sup=new Supervision();
						sup.setSid(supervisionApply.getSupId());
						sup.setStatus(5);
						sup.setRealEndTime(DateFormat.getCurrentTime());
						int b=supervisionMapper.updateByPrimaryKeySelective(sup);
						if(b<1){
							return toJson;
						}
					}
					break;
				default:
					break;
			}
            //int a=supervisionMapper.updateSup(supervision);
            //状态改变了，向申请表里增加一条新数据
            int b=supervisionApplyMapper.addSupervisionApply(supervisionApply);
            if(b>0){
            	toJson.setFlag(0);
                toJson.setMsg("ok");
            }else {
            	toJson.setFlag(1);
                toJson.setMsg("新增失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }


    public ToJson<SupervisionApply> updateStatus(HttpServletRequest request,SupervisionApply supervisionApply){
        ToJson<SupervisionApply> toJson=new ToJson<SupervisionApply>(1,"error");
        try{
        	//  1 暂停申请 2 恢复申请 3办结申请  4任务催 办
		    int abc=supervisionApply.getType();
        	switch (abc) {
			case 1:
				if(supervisionApply.getStatus()==1){
					Supervision sup=new Supervision();
					sup.setSid(supervisionApply.getSupId());
					sup.setStatus(3);
					 int a=supervisionMapper.updateByPrimaryKeySelective(sup);
					 if(a<1){
						 return toJson;
					 }
				}else{
					Supervision sup=new Supervision();
					sup.setSid(supervisionApply.getSupId());
					sup.setStatus(1);
					 int a=supervisionMapper.updateByPrimaryKeySelective(sup);
					 if(a<1){
						 return toJson;
					 }
				}
					 int a=supervisionApplyMapper.updateStatus(supervisionApply);
					 if(a<1){
						 return toJson;
					 }
				break;
            case 2:
            	if(supervisionApply.getStatus()==1){
					Supervision sup=new Supervision();
					sup.setSid(supervisionApply.getSupId());
					sup.setStatus(1);
					 int e=supervisionMapper.updateByPrimaryKeySelective(sup);
					 if(e<1){
						 return toJson;
					 }
				}else{
					Supervision sup=new Supervision();
					sup.setSid(supervisionApply.getSupId());
					sup.setStatus(3);
					 int e=supervisionMapper.updateByPrimaryKeySelective(sup);
					 if(e<1){
						 return toJson;
					 }
				}
					 int e=supervisionApplyMapper.updateStatus(supervisionApply);
					 if(e<1){
						 return toJson;
					 }
				break;
            case 3:
            	if(supervisionApply.getStatus()==1){
					Supervision sup=new Supervision();
					sup.setSid(supervisionApply.getSupId());
					sup.setStatus(6);
					sup.setRealEndTime(DateFormat.getCurrentTime());
					 int b=supervisionMapper.updateByPrimaryKeySelective(sup);
					 if(b<1){
						 return toJson;
					 }
				}else if(supervisionApply.getStatus()==3){
					Supervision sup=new Supervision();
					sup.setSid(supervisionApply.getSupId());
					sup.setStatus(1);
					 int b=supervisionMapper.updateByPrimaryKeySelective(sup);
					 if(b<1){
						 return toJson;
					 }
				}else{
					
				}
					 int b=supervisionApplyMapper.updateStatus(supervisionApply);
					 if(b<1){
						 return toJson;
					 }
				break;
            case 4:
            	if(supervisionApply.getStatus()==1){
					Supervision sup=new Supervision();
					sup.setSid(supervisionApply.getSupId());
					sup.setStatus(4);
					 int c=supervisionMapper.updateByPrimaryKeySelective(sup);
					 if(c<1){
						 return toJson;
					 }
				}else{
					Supervision sup=new Supervision();
					sup.setSid(supervisionApply.getSupId());
					sup.setStatus(3);
					int c=supervisionMapper.updateByPrimaryKeySelective(sup);
				    if(c<1){
						 return toJson;
					}
				}
					 int c=supervisionApplyMapper.updateStatus(supervisionApply);
					 if(c<1){
						 return toJson;
					 }
				break;
			default:
				break;
			}
        	
        	toJson.setFlag(0);
        	toJson.setMsg("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }


    public  ToJson<SupervisionApply> getStatusInfor(HttpServletRequest request,Integer status){
        ToJson<SupervisionApply> toJson=new ToJson<SupervisionApply>(1,"error");
        try{
        	String createrId=(String)request.getSession().getAttribute("userId");
        	SupervisionApply sup=new SupervisionApply();
        	sup.setCreaterId(createrId);
            List<SupervisionApply> list=supervisionApplyMapper.getStatusInfor(sup);
            if(list.size()>0){
                toJson.setObj(list);
                toJson.setFlag(0);
                toJson.setMsg("OK");
            }else {
                toJson.setMsg("err");
            }
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }


	
	@Override
	public ToJson<SupervisionApply> getSupApplyByStatus(HttpServletRequest request, SupervisionApply supervisionApply) {
		 ToJson<SupervisionApply> toJson=new ToJson<SupervisionApply>(1,"error");
	        try{
	        	supervisionApply.setCreaterId((String) request.getSession().getAttribute("userId"));
	            List<SupervisionApply> list=supervisionApplyMapper.getStatusInfor(supervisionApply);
	            if(list.size()>0){
	                toJson.setObj(list);
	                toJson.setFlag(0);
	                toJson.setMsg("OK");
	            }else {
	                toJson.setMsg("err");
	            }
	            toJson.setFlag(0);
	        }catch (Exception e){
	            e.printStackTrace();
	            toJson.setMsg(e.getMessage());
	        }
	        return toJson;
	}
}
